//
//  OtherManagementController.m
//  tuanGG
//
//  Created by 微他 on 15/9/21.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "OtherManagementController.h"
#import "Header.h"
#import "ValidationCodeViewController.h"
#import "OtherManagementViewCell.h"
@interface OtherManagementController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@property (strong, nonatomic)UISegmentedControl *segControl;
@property (strong, nonatomic)UIView *line;
@property (strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)UIView *headView;
@property (strong, nonatomic)UISearchBar *search;
@property (strong, nonatomic)NSArray *dataArray;
@property (strong, nonatomic)NSArray *array;
@property (assign, nonatomic)NSInteger index;
@property (assign, nonatomic)NSInteger number;
@property (assign, nonatomic)NSInteger page;
@property (nonatomic,strong)NSArray *dataArr;//数据源
@property (nonatomic,strong)NSMutableArray *resultsArr;//搜索结果
@end

@implementation OtherManagementController

- (void)viewWillAppear:(BOOL)animated{
    [self tabBarShow];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"验证订单号" style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    NSDictionary *rightTitle = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [rightItem setTitleTextAttributes:rightTitle forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeDataSource];
    [self initView];
    [self NetWorking];
    [self addTableView];

}
- (void)initlizeDataSource{
    _index = 1;
    _number = 5;
    _page = 1;
    _dataArr = [NSArray array];;
    _dataArray = [NSArray array];
    _array = @[@"",@"未验证",@"",@"",@"",@"已验证"];
}
- (void)initView {
    [self addnavigationTitle:@"订单管理" navColor:@"4fd2be"];
    
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 85)];
    _search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    _search.placeholder = @"搜索单号、用户名";
    _search.delegate =self;
    [_headView addSubview:_search];
    
    NSArray *array = @[@"已消费",@"未消费"];
    _segControl = [[UISegmentedControl alloc]initWithItems:array];
    _segControl.frame = CGRectMake(0,_search.frame.size.height, ScreenWidth, 45) ;
    _segControl.tintColor = [UIColor clearColor];
    _segControl.selectedSegmentIndex = 0;
    _segControl.backgroundColor = [UIColor whiteColor];
    //选中状态
    NSDictionary *selectedTextttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"969696"]};
    [_segControl setTitleTextAttributes:selectedTextttributes forState:UIControlStateSelected];
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
                                               NSForegroundColorAttributeName: [UIColor colorWithHexString:@"969696"]};
    [_segControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.view addSubview:_segControl];
    [_segControl addTarget:self action:@selector(otherButton:) forControlEvents:UIControlEventValueChanged];
    
    _line = [[UIView alloc]initWithFrame:CGRectMake(0, 46, ScreenWidth/2, 2)];
    _line.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    [_segControl addSubview:_line];
    
    UIView *lines = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, _segControl.frame.size.height-10)];
    lines.backgroundColor = [UIColor blackColor];
    lines.alpha = 0.1;
    lines.center = CGPointMake(ScreenWidth/2, _segControl.frame.size.height/2);
    [_segControl addSubview:lines];
    [_headView addSubview:_segControl];
    [self.view addSubview:_headView];
}
- (void)addTableView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _headView.frame.size.height+3, ScreenWidth, ScreenHeight-165) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [_tableView registerClass:[OtherManagementViewCell class] forCellReuseIdentifier:@"CELL1"];
    [_tableView registerClass:[OtherManagementViewCell class] forCellReuseIdentifier:@"CELL2"];
//    _tableView.tableHeaderView = _headView;
    [self.view addSubview:_tableView];
}
#pragma mark - 事件实现
- (void)headerRereshing{
    _page = 1;
    _number = 5*_page;
    [self NetWorking];
}
- (void)footerRereshing{
    _page ++;
    _number = 5*_page;
    [self NetWorking];
}
- (void)rightItem {
    ValidationCodeViewController *ValidationCode = [[ValidationCodeViewController alloc]init];
    [self.navigationController pushViewController:ValidationCode animated:YES];
    
}
- (void)otherButton:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        NSLog(@"已消费");
        _index = 1;
        [self NetWorking];
        [self alreadyshelves];
    }else if (sender.selectedSegmentIndex == 1 ){
        _index = 0;
        NSLog(@"未消费");
        [self NetWorking];
        [self noShelves];
    }
}
- (void)alreadyshelves
{
    //已消费
    [UIView animateWithDuration:0.1 animations:^{
        _line.frame = CGRectMake(0, 46, ScreenWidth / 2, 2);
    }];
    
    
    
}

- (void)noShelves
{
    //未消费
    [UIView animateWithDuration:0.1 animations:^{
        _line.frame = CGRectMake(ScreenWidth / 2, 46, ScreenWidth / 2, 2);
    }];
}
#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",_dataArray);
    OtherManagementViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL1" forIndexPath:indexPath];
    if (_segControl.selectedSegmentIndex == 0 ) {
        [cell.HeadImage sd_setImageWithURL:[NSURL URLWithString:_dataArr[indexPath.row][@"avatarm"]] placeholderImage:nil];
        cell.nameLabel.text = [NSString stringWithFormat:@"%@",_dataArr[indexPath.row][@"username"]];
        cell.priceLabel.text = [NSString stringWithFormat:@"订单号:%@",_dataArr[indexPath.row][@"consumekey"]];
//        NSLog(@"%ld",[_dataArray[indexPath.row][@"status"]integerValue]);
        cell.validationLabel.text = [NSString stringWithFormat:@"总价:%@元  %@",_dataArr[indexPath.row][@"price"],_array[[_dataArr[indexPath.row][@"status"]integerValue]]];
    }else{
        [cell.HeadImage sd_setImageWithURL:[NSURL URLWithString:_dataArr[indexPath.row][@"avatarm"]] placeholderImage:nil];
        cell.nameLabel.text = [NSString stringWithFormat:@"%@",_dataArr[indexPath.row][@"username"]];
        cell.priceLabel.text = [NSString stringWithFormat:@"总价:%@元",_dataArr[indexPath.row][@"price"]];
        NSLog(@"%ld",[_dataArr[indexPath.row][@"status"]integerValue]);
        cell.validationLabel.text = _array[[_dataArr[indexPath.row][@"status"]integerValue]];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_search endEditing:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [ self.view endEditing:YES];
}
#pragma -mark  <UIsearchdelegate>
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    NSMutableArray * results=[[NSMutableArray alloc]init];

    if (_dataArray.count != 0) {

        for (int i = 0 ; i < _dataArray.count ; i++) {
            NSString *string = [_dataArray[i][@"consumekey"]mutableCopy];
            NSString *string1 = [_dataArray[i][@"merchname"]mutableCopy];
            if ([string rangeOfString:_search.text].location != NSNotFound||[string1 rangeOfString:_search.text].location != NSNotFound) {
                [results addObject:_dataArray[i]];
            }
        }
        NSLog(@"%@",results);
        _dataArr = [NSArray array];
        _dataArr = [results copy];
        [_tableView reloadData];
    }else {
        [Common addAlert:@"没有数据可查询"];
    }

}
#pragma mark - 网络请求
- (void)NetWorking {
    NSDictionary *dictionary = @{@"type":[NSString stringWithFormat:@"%ld",_index],@"page":[NSString stringWithFormat:@"%ld",_page],@"size":[NSString stringWithFormat:@"%ld",_number]};
    [GLAFRequest startRequest:order_list_url paramsDic:dictionary defaultdialog:YES ispost:NO handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"数据%@",sta);
        if ([sta[@"code"]integerValue ] == 1) {
            [_tableView headerEndRefreshing];
            [_tableView footerEndRefreshing];
            _dataArray = [sta[@"iteminfo"]mutableCopy];
            _dataArr = [_dataArray copy];
            [_tableView reloadData];
        }else {
            [_tableView headerEndRefreshing];
            [_tableView footerEndRefreshing];
        }

    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
@end
